#include "ObjectHook.hpp"
#include <iostream>

using std::cerr;
using std::endl;

void ObjectHook::objectToBuffer(const TObject *, google::protobuf::Message *)
{
    cerr<<"Error when invoking ObjectHook::objectToBuffer"<<endl;
}

void ObjectHook::bufferToObject(const google::protobuf::Message *, TObject *)
{
    cerr<<"Error when invoking ObjectHook::bufferToObject"<<endl;
}

void ObjectHook::bufferToObject(const bes3::TEvtRecVeeVertex *buf,
                                TEvtRecVeeVertex *veevtx)
{
    veevtx->m_vertexId=buf->m_vertexid();
    veevtx->m_vertexType=buf->m_vertextype();
    veevtx->m_chi2=buf->m_chi2();
    veevtx->m_ndof=buf->m_ndof();
    veevtx->m_mass=buf->m_mass();
    for(int i=0;i<28;i++){
        if (i<7){
            veevtx->m_w[i]=buf->m_w(i);
        }
        veevtx->m_Ew[i]=buf->m_ew(i);
    }
    veevtx->m_nCharge=buf->m_ncharge();
    veevtx->m_nTracks=buf->m_ntracks();
    for(int i=0;i<2;i++){
        veevtx->m_pair[i]=buf->m_pair(i);
        veevtx->m_daughter[i]=buf->m_daughter(i);
    }
}

void ObjectHook::bufferToObject(const bes3::TEvtRecPi0 *buf, TEvtRecPi0 *pi0)
{
    pi0->m_unconMass=buf->m_unconmass();
    pi0->m_chisq=buf->m_chisq();
    pi0->m_hiPx=buf->m_hipx();
    pi0->m_hiPy=buf->m_hipy();
    pi0->m_hiPz=buf->m_hipz();
    pi0->m_hiPe=buf->m_hipe();
    pi0->m_loPx=buf->m_lopx();
    pi0->m_loPy=buf->m_lopy();
    pi0->m_loPz=buf->m_lopz();
    pi0->m_loPe=buf->m_lope();
    pi0->m_hiEnGamma=buf->m_hiengamma();
    pi0->m_loEnGamma=buf->m_loengamma();
}

void ObjectHook::bufferToObject(const bes3::TEvtRecDTag *buf, TEvtRecDTag *dtag)
{
    dtag->m_decayMode=buf->m_decaymode();
    dtag->m_type=buf->m_type();
    dtag->m_beamE=buf->m_beame();
    dtag->m_mass=buf->m_mass();
    dtag->m_mBC=buf->m_mbc();
    dtag->m_deltaE=buf->m_deltae();
    dtag->m_charge=buf->m_charge();
    dtag->m_charm=buf->m_charm();
    dtag->m_numOfChildren=buf->m_numofchildren();
    dtag->m_px=buf->m_px();
    dtag->m_py=buf->m_py();
    dtag->m_pz=buf->m_pz();
    dtag->m_pe=buf->m_pe();
    dtag->m_tracks.clear();
    for(int i=0,total=buf->m_tracks_size();i<total;i++){
        dtag->m_tracks.push_back(buf->m_tracks(i));
    }
    dtag->m_showers.clear();
    for(int i=0,total=buf->m_showers_size();i<total;i++){
        dtag->m_showers.push_back(buf->m_showers(i));
    }
    dtag->m_otherTracks.clear();
    for(int i=0,total=buf->m_othertracks_size();i<total;i++){
        dtag->m_otherTracks.push_back(buf->m_othertracks(i));
    }
    dtag->m_otherShowers.clear();
    for(int i=0,total=buf->m_othershowers_size();i<total;i++){
        dtag->m_otherShowers.push_back(buf->m_othershowers(i));
    }
    dtag->m_pionId.clear();
    for(int i=0,total=buf->m_pionid_size();i<total;i++){
        dtag->m_pionId.push_back(buf->m_pionid(i));
    }
    dtag->m_kaonId.clear();
    for(int i=0,total=buf->m_kaonid_size();i<total;i++){
        dtag->m_kaonId.push_back(buf->m_kaonid(i));
    }
}

void ObjectHook::bufferToObject(const bes3::TTrigData *buf, TTrigData *trigData)
{
    trigData->m_preScale=buf->m_prescale();
    for(int i=0;i<48;++i){
        trigData->m_trigcond[i]=buf->m_trigcond(i);
        if (i<16){
            trigData->m_trigChan[i]=buf->m_trigchan(i);
        }
    }
    trigData->m_timeWindow=buf->m_timewindow();
    trigData->m_Timing=buf->m_timing();
}

void ObjectHook::bufferToObject(const bes3::THltEvent *buf, THltEvent *hltevt)
{
    hltevt->m_fromMc=buf->m_frommc();
    for(int i=0,total=buf->m_hltrawcol_size();i<total;i++){
        const bes3::THltRaw &hltraw=buf->m_hltrawcol(i);
        THltRaw *rawdata=new THltRaw;
        bufferToObject(&hltraw,rawdata);
        hltevt->m_hltRawCol->Add(rawdata);
    }
    const bes3::THltInf &inf=buf->m_hltinf();
    bufferToObject(&inf,hltevt->m_hltInf);
    const bes3::TDstHltInf &dstinf=buf->m_dsthltinf();
    bufferToObject(&dstinf,hltevt->m_dstHltInf);
}

void ObjectHook::bufferToObject(const bes3::TDstEvent *buf, TDstEvent *dstEvt)
{
    dstEvt->m_emcTrackCol->Clear();
    for(int i=0,total=buf->m_emctrackcol_size();i<total;i++){
        const bes3::TEmcTrack &bes3emctrack=buf->m_emctrackcol(i);
        TEmcTrack *track=new TEmcTrack;
        bufferToObject(&bes3emctrack,track);
        dstEvt->m_emcTrackCol->Add(track);
    }
    dstEvt->m_mdcTrackCol->Clear();
    for(int i=0,total=buf->m_emctrackcol_size();i<total;i++){
        const bes3::TMdcTrack &bes3mdctrack=buf->m_mdctrackcol(i);
        TMdcTrack *track=new TMdcTrack;
        bufferToObject(&bes3mdctrack, track);
        dstEvt->m_mdcTrackCol->Add(track);
    }
    dstEvt->m_tofTrackCol->Clear();
    for(int i=0,total=buf->m_toftrackcol_size();i<total;i++){
        const bes3::TTofTrack &bes3toftrack=buf->m_toftrackcol(i);
        TTofTrack *track=new TTofTrack;
        bufferToObject(&bes3toftrack,track);
        dstEvt->m_tofTrackCol->Add(track);
    }
    dstEvt->m_mucTrackCol->Clear();
    for(int i=0,total=buf->m_muctrackcol_size();i<total;i++){
        const bes3::TMucTrack &bes3muctrack=buf->m_muctrackcol(i);
        TMucTrack *track=new TMucTrack;
        bufferToObject(&bes3muctrack,track);
        dstEvt->m_mucTrackCol->Add(track);
    }
    dstEvt->m_mdcDedxCol->Clear();
    for(int i=0,total=buf->m_mdcdedxcol_size();i<total;i++){
        const bes3::TMdcDedx &bes3dedx=buf->m_mdcdedxcol(i);
        TMdcDedx *dedx=new TMdcDedx;
        bufferToObject(&bes3dedx,dedx);
        dstEvt->m_mdcDedxCol->Add(dedx);
    }
    dstEvt->m_extTrackCol->Clear();
    for(int i=0,total=buf->m_exttrackcol_size();i<total;i++){
        const bes3::TExtTrack &bes3exttrack=buf->m_exttrackcol(i);
        TExtTrack *track=new TExtTrack;
        bufferToObject(&bes3exttrack,track);
        dstEvt->m_extTrackCol->Add(track);
    }
    dstEvt->m_mdcKalTrackCol->Clear();
    for(int i=0,total=buf->m_exttrackcol_size();i<total;i++){
        const bes3::TMdcKalTrack &bes3mdckal=buf->m_mdckaltrackcol(i);
        TMdcKalTrack *track=new TMdcKalTrack;
        bufferToObject(&bes3mdckal,track);
        dstEvt->m_mdcKalTrackCol->Add(track);
    }
}

void ObjectHook::bufferToObject(const bes3::TTofTrack *buf, TTofTrack *toftrack)
{
    toftrack->m_tofTrackID=buf->m_toftrackid();
    toftrack->m_trackID=buf->m_trackid();
    toftrack->m_tofID=buf->m_tofid();
    toftrack->m_status=buf->m_status();
    toftrack->m_path=buf->m_path();
    toftrack->m_zrhit=buf->m_zrhit();
    toftrack->m_ph=buf->m_ph();
    toftrack->m_tof=buf->m_tof();
    toftrack->m_beta=buf->m_beta();
    for(int i=0;i<6;i++){
        if (i<5){
            toftrack->m_texp[i]=buf->m_texp(i);
        }
        toftrack->m_toffset[i]=buf->m_toffset(i);
        toftrack->m_sigma[i]=buf->m_sigma(i);
    }
    toftrack->m_quality=buf->m_quality();
    toftrack->m_t0=buf->m_t0();
    toftrack->m_errt0=buf->m_errt0();
    toftrack->m_errz=buf->m_errtz();
    toftrack->m_phi=buf->m_phi();
    toftrack->m_errphi=buf->m_errphi();
    toftrack->m_errenergy=buf->m_errenergy();
}

void ObjectHook::bufferToObject(const bes3::TMdcDedx *buf, TMdcDedx *dedx)
{
    dedx->m_trackId=buf->m_trackid();
    dedx->m_particleId=buf->m_particleid();
    dedx->m_status=buf->m_status();
    dedx->m_trunc_alg=buf->m_trunc_alg();
    dedx->m_chiE=buf->m_chie();
    dedx->m_chiMu=buf->m_chimu();
    dedx->m_chiPi=buf->m_chipi();
    dedx->m_chiK=buf->m_chik();
    dedx->m_chiP=buf->m_chip();
    dedx->m_numGoodHits=buf->m_numgoodhits();
    dedx->m_numTotalHits=buf->m_numtotalhits();
    dedx->m_probPH=buf->m_probph();
    dedx->m_normPH=buf->m_normph();
    dedx->m_errorPH=buf->m_errorph();
    dedx->m_twentyPH=buf->m_twentyph();
    for(int i=0;i<5;i++){
        dedx->m_chi[i]=buf->m_chi(i);
    }
}

void ObjectHook::bufferToObject(const bes3::TExtTrack *buf, TExtTrack *ext)
{
    ext->m_trackId=buf->m_trackid();
    ext->myTof1PositionX=buf->mytof1positionx();
    ext->myTof1PositionY=buf->mytof1positiony();
    ext->myTof1PositionZ=buf->mytof1positionz();
    ext->myTof1MomentumX=buf->mytof1momentumx();
    ext->myTof1MomentumY=buf->mytof1momentumy();
    ext->myTof1MomentumZ=buf->mytof1momentumz();
    ext->myTof1VolumeName=TString(buf->mytof1volumename());
    ext->myTof1VolumeNumber=buf->mytof1volumenumber();
    ext->myTof1=buf->mytof1();
    ext->myTof1Path=buf->mytof1path();
    ext->myTof1PosSigmaAlongZ=buf->mytof1possigmaalongz();
    ext->myTof1PosSigmaAlongT=buf->mytof1possigmaalongt();
    ext->myTof1PosSigmaAlongX=buf->mytof1possigmaalongx();
    ext->myTof1PosSigmaAlongY=buf->mytof1possigmaalongy();

    ext->myTof2PositionX=buf->mytof2positionx();
    ext->myTof2PositionY=buf->mytof2positiony();
    ext->myTof2PositionZ=buf->mytof2positionz();
    ext->myTof2MomentumX=buf->mytof2momentumx();
    ext->myTof2MomentumY=buf->mytof2momentumy();
    ext->myTof2MomentumZ=buf->mytof2momentumz();
    ext->myTof2VolumeName=TString(buf->mytof2volumename());
    ext->myTof2VolumeNumber=buf->mytof2volumenumber();
    ext->myTof2=buf->mytof2();
    ext->myTof2Path=buf->mytof2path();
    ext->myTof2PosSigmaAlongZ=buf->mytof2possigmaalongz();
    ext->myTof2PosSigmaAlongT=buf->mytof2possigmaalongt();
    ext->myTof2PosSigmaAlongX=buf->mytof2possigmaalongx();
    ext->myTof2PosSigmaAlongY=buf->mytof2possigmaalongy();

    ext->myEmcPositionX=buf->myemcpositionx();
    ext->myEmcPositionY=buf->myemcpositiony();
    ext->myEmcPositionZ=buf->myemcpositionz();
    ext->myEmcMomentumX=buf->myemcmomentumx();
    ext->myEmcMomentumY=buf->myemcmomentumy();
    ext->myEmcMomentumZ=buf->myemcmomentumz();
    ext->myEmcVolumeName=TString(buf->myemcvolumename());
    ext->myEmcVolumeNumber=buf->myemcvolumenumber();
    ext->myEmcPosSigmaAlongTheta=buf->myemcpossigmaalongtheta();
    ext->myEmcPosSigmaAlongPhi=buf->myemcpossigmaalongphi();

    ext->myEmcPath=buf->myemcpath();
    ext->myMucPositionX=buf->mymucpositionx();
    ext->myMucPositionY=buf->mymucpositiony();
    ext->myMucPositionZ=buf->mymucpositionz();
    ext->myMucMomentumX=buf->mymucmomentumx();
    ext->myMucMomentumY=buf->mymucmomentumy();
    ext->myMucMomentumZ=buf->mymucmomentumz();
    ext->myMucVolumeName=TString(buf->mymucvolumename());
    ext->myMucVolumeNumber=buf->mymucvolumenumber();
    ext->myMucPosSigmaAlongZ=buf->mymucpossigmaalongz();
    ext->myMucPosSigmaAlongT=buf->mymucpossigmaalongt();
    ext->myMucPosSigmaAlongX=buf->mymucpossigmaalongx();
    ext->myMucPosSigmaAlongY=buf->mymucpossigmaalongy();

    for(int i=0,j=0,k=0;i<36;++i){
        ext->myTof1ErrorMatrix[j][k]=buf->mytof1errormatrix(i);
        ext->myTof2ErrorMatrix[j][k]=buf->mytof2errormatrix(i);
        ext->myEmcErrorMatrix[j][k]=buf->myemcerrormatrix(i);
        ext->myMucErrorMatrix[j][k]=buf->mymucerrormatrix(i);
        ++k;
        if(k==6){
            ++j;
            k=0;
        }
    }
}

void ObjectHook::bufferToObject(const bes3::TMdcKalTrack *buf, TMdcKalTrack *mdckal)
{
    mdckal->m_trackId=buf->m_trackid();
    for (int i=0;i<5;i++){
        mdckal->m_stat[i]=buf->m_stat(i);
        mdckal->m_stat2[i]=buf->m_stat2(i);
        mdckal->m_chisq[i]=buf->m_chisq(i);
        mdckal->m_ndf[i]=buf->m_ndf(i);
        mdckal->m_nster[i]=buf->m_nster(i);
        mdckal->m_firstLayer[i]=buf->m_firstlayer(i);
        mdckal->m_lastLayer[i]=buf->m_lastlayer(i);
        mdckal->m_zhelix[i]=buf->m_zhelix(i);
        mdckal->m_zhelix_e[i]=buf->m_zhelix_e(i);
        mdckal->m_zhelix_mu[i]=buf->m_zhelix_mu(i);
        mdckal->m_zhelix_k[i]=buf->m_zhelix_k(i);
        mdckal->m_zhelix_p[i]=buf->m_zhelix_p(i);
        mdckal->m_fhelix[i]=buf->m_fhelix(i);
        mdckal->m_fhelix_e[i]=buf->m_fhelix_e(i);
        mdckal->m_fhelix_mu[i]=buf->m_fhelix_mu(i);
        mdckal->m_fhelix_k[i]=buf->m_fhelix_k(i);
        mdckal->m_fhelix_p[i]=buf->m_fhelix_p(i);
        for(int j=0;j<5;j++){
            int idx=i*5+j;
            mdckal->m_zerror[i][j]=buf->m_zerror(idx);
            mdckal->m_zerror_e[i][j]=buf->m_zerror_e(idx);
            mdckal->m_zerror_mu[i][j]=buf->m_zerror_mu(idx);
            mdckal->m_zerror_k[i][j]=buf->m_zerror_k(idx);
            mdckal->m_zerror_p[i][j]=buf->m_zerror_p(idx);
            mdckal->m_ferror[i][j]=buf->m_ferror(idx);
            mdckal->m_ferror_e[i][j]=buf->m_ferror_e(idx);
            mdckal->m_ferror_mu[i][j]=buf->m_ferror_mu(idx);
            mdckal->m_ferror_k[i][j]=buf->m_ferror_k(idx);
            mdckal->m_ferror_p[i][j]=buf->m_ferror_p(idx);
        }
    }
}

void ObjectHook::bufferToObject(const bes3::THltInf *buf, THltInf *inf)
{
    inf->m_evtType=buf->m_evttype();
    inf->m_algProcess=buf->m_algprocess();
    inf->m_criTable=buf->m_critable();
    inf->m_verNumber=buf->m_vernumber();
    inf->m_eTotal=buf->m_etotal();
    inf->m_subNumber=buf->m_subnumber();
    inf->m_conNumber=buf->m_connumber();
    inf->m_mdcData.clear();
    for(int i=0,total=buf->m_mdcdata_size();i<total;i++){
        inf->m_mdcData.push_back(buf->m_mdcdata(i));
    }
    inf->m_tofData.clear();
    for(int i=0,total=buf->m_tofdata_size();i<total;i++){
        inf->m_tofData.push_back(buf->m_tofdata(i));
    }
    inf->m_emcData.clear();
    for(int i=0,total=buf->m_emcdata_size();i<total;i++){
        inf->m_emcData.push_back(buf->m_emcdata(i));
    }
    inf->m_mucData.clear();
    for(int i=0,total=buf->m_mucdata_size();i<total;i++){
        inf->m_mucData.push_back(buf->m_mucdata(i));
    }
    inf->m_conData.clear();
    for(int i=0,total=buf->m_condata_size();i<total;i++){
        inf->m_conData.push_back(buf->m_condata(i));
    }
}

void ObjectHook::bufferToObject(const bes3::TDstHltInf *buf, TDstHltInf *dstinf)
{
    dstinf->m_evtType=buf->m_evttype();
    dstinf->m_algProcess=buf->m_algprocess();
    dstinf->m_criTable=buf->m_critable();
    dstinf->m_verNumber=buf->m_vernumber();
    dstinf->m_eTotal=buf->m_etotal();
    dstinf->m_subNumber=buf->m_subnumber();
    dstinf->m_conNumber=buf->m_connumber();
}

void ObjectHook::bufferToObject(const bes3::THltRaw *buf, THltRaw *hltraw)
{
    const bes3::TRawData &rawdata=buf->base();
    bufferToObject(&rawdata,hltraw);
}

void ObjectHook::bufferToObject(const bes3::TMdcMc *buf, TMdcMc *mdcmc)
{
    mdcmc->m_id=buf->m_id();
    mdcmc->m_trackIndex=buf->m_trackindex();
    mdcmc->m_xPosition=buf->m_xposition();
    mdcmc->m_yPosition=buf->m_yposition();
    mdcmc->m_zPosition=buf->m_zposition();
    mdcmc->m_driftDistance=buf->m_driftdistance();
    mdcmc->m_depositEnergy=buf->m_depositenergy();
    mdcmc->m_posFlag=buf->m_posflag();
}

void ObjectHook::bufferToObject(const bes3::TMucMc *buf, TMucMc *mucmc)
{
    mucmc->m_id=buf->m_id();
    mucmc->m_trackIndex=buf->m_trackindex();
    mucmc->m_xPosition=buf->m_xposition();
    mucmc->m_yPosition=buf->m_yposition();
    mucmc->m_zPosition=buf->m_zposition();
    mucmc->m_px=buf->m_px();
    mucmc->m_py=buf->m_py();
    mucmc->m_pz=buf->m_pz();
    mucmc->m_depositEnergy=buf->m_depositenergy();
}

void ObjectHook::bufferToObject(const bes3::TEmcMc *buf, TEmcMc *emcmc)
{
    emcmc->m_hitEmc=buf->m_hitemc();
    emcmc->m_PDGCode=buf->m_pdgcode();
    emcmc->m_PDGCharge=buf->m_pdgcharge();
    emcmc->m_time=buf->m_time();
    emcmc->m_id=buf->m_id();
    emcmc->m_trackIndex=buf->m_trackindex();
    emcmc->m_xPosition=buf->m_xposition();
    emcmc->m_yPosition=buf->m_yposition();
    emcmc->m_zPosition=buf->m_zposition();
    emcmc->m_px=buf->m_px();
    emcmc->m_py=buf->m_py();
    emcmc->m_pz=buf->m_pz();
    emcmc->m_depositEnergy=buf->m_depositenergy();
    emcmc->m_hitMap.clear();
    typedef std::pair<int,double> HitMapEntry;
    for(int i=0,total=buf->m_hitmap_size();i<total;i++){
        const bes3::TEmcMc_HitMapEntry &entry=buf->m_hitmap(i);
        emcmc->m_hitMap.insert(HitMapEntry(entry.key(),entry.value()));
    }
}

void ObjectHook::bufferToObject(const bes3::TTofMc *buf, TTofMc *tofmc)
{
    tofmc->m_id=buf->m_id();
    tofmc->m_trackIndex=buf->m_trackindex();
    tofmc->m_xPosition=buf->m_xposition();
    tofmc->m_yPosition=buf->m_yposition();
    tofmc->m_zPosition=buf->m_zposition();
    tofmc->m_px=buf->m_px();
    tofmc->m_py=buf->m_py();
    tofmc->m_pz=buf->m_pz();
    tofmc->m_trackLength=buf->m_tracklength();
    tofmc->m_flightTime=buf->m_flighttime();
}

void ObjectHook::bufferToObject(const bes3::TMcParticle *buf, TMcParticle *mcp)
{
    mcp->m_particleID=buf->m_particleid();
    mcp->m_trackIndex=buf->m_trackindex();
    mcp->m_vertexIndex0=buf->m_vertexindex0();
    mcp->m_vertexIndex1=buf->m_vertexindex1();
    mcp->m_statusFlags=buf->m_statusflags();
    mcp->m_xInitialPosition=buf->m_xinitialposition();
    mcp->m_yInitialPosition=buf->m_yinitialposition();
    mcp->m_zInitialPosition=buf->m_zinitialposition();
    mcp->m_tInitialPosition=buf->m_tinitialposition();
    mcp->m_xFinalPosition=buf->m_xfinalposition();
    mcp->m_yFinalPosition=buf->m_yfinalposition();
    mcp->m_zFinalPosition=buf->m_zfinalposition();
    mcp->m_tFinalPosition=buf->m_tfinalposition();
    mcp->m_xInitialMomentum=buf->m_xinitialmomentum();
    mcp->m_yInitialMomentum=buf->m_yinitialmomentum();
    mcp->m_zInitialMomentum=buf->m_zinitialmomentum();
    mcp->m_eInitialMomentum=buf->m_tinitialmomentum();
    mcp->m_mother=buf->m_mother();
    mcp->m_daughters.clear();
    for(int i=0,total=buf->m_daughters_size();i<total;i++){
        mcp->m_daughters.push_back(buf->m_daughters(i));
    }
}

void ObjectHook::bufferToObject(const bes3::TDigiEvent *buf, TDigiEvent *digiEvt)
{
    digiEvt->m_fromMc=buf->m_frommc();
    digiEvt->m_mdcDigiCol->Clear();
    digiEvt->m_emcDigiCol->Clear();
    digiEvt->m_tofDigiCol->Clear();
    digiEvt->m_mucDigiCol->Clear();
    digiEvt->m_lumiDigiCol->Clear();
    int i,total;
    for(i=0,total=buf->m_mdcdigicol_size();i<total;i++){
        const bes3::TMdcDigi &bes3digi=buf->m_mdcdigicol(i);
        TMdcDigi *digi=new TMdcDigi;
        bufferToObject(&bes3digi,digi);
        digiEvt->m_mdcDigiCol->Add(digi);
    }
    for(i=0,total=buf->m_emcdigicol_size();i<total;i++){
        const bes3::TEmcDigi &bes3digi=buf->m_emcdigicol(i);
        TEmcDigi *digi=new TEmcDigi;
        bufferToObject(&bes3digi,digi);
        digiEvt->m_emcDigiCol->Add(digi);
    }
    for(i=0,total=buf->m_tofdigicol_size();i<total;i++){
        const bes3::TTofDigi &bes3digi=buf->m_tofdigicol(i);
        TTofDigi *digi=new TTofDigi;
        bufferToObject(&bes3digi,digi);
        digiEvt->m_tofDigiCol->Add(digi);
    }
    for(i=0,total=buf->m_mucdigicol_size();i<total;i++){
        const bes3::TMucDigi &bes3digi=buf->m_mucdigicol(i);
        TMucDigi *digi=new TMucDigi;
        bufferToObject(&bes3digi,digi);
        digiEvt->m_mucDigiCol->Add(digi);
    }
    for(i=0,total=buf->m_lumidigicol_size();i<total;i++){
        const bes3::TLumiDigi &bes3digi=buf->m_lumidigicol(i);
        TLumiDigi *digi=new TLumiDigi;
        bufferToObject(&bes3digi,digi);
        digiEvt->m_lumiDigiCol->Add(digi);
    }
}

void ObjectHook::bufferToObject(const bes3::TTrigEvent *buf, TTrigEvent *trigEvt)
{
    trigEvt->m_fromMc=buf->m_frommc();
    const bes3::TTrigData &trigData=buf->m_trigdata();
    bufferToObject(&trigData,trigEvt->m_trigData);
}

void ObjectHook::bufferToObject(const bes3::TMcEvent *buf, TMcEvent *mcevt)
{
    mcevt->m_mdcMcHitCol->Clear();
    for(int i=0,total=buf->m_mdcmchitcol_size();i<total;i++){
        const bes3::TMdcMc &bes3mdcmc=buf->m_mdcmchitcol(i);
        TMdcMc *mdcmc=new TMdcMc;
        bufferToObject(&bes3mdcmc,mdcmc);
        mcevt->m_mdcMcHitCol->Add(mdcmc);
    }
    mcevt->m_emcMcHitCol->Clear();
    for(int i=0,total=buf->m_emcmchitcol_size();i<total;i++){
        const bes3::TEmcMc &bes3emcmc=buf->m_emcmchitcol(i);
        TEmcMc *emcmc=new TEmcMc;
        bufferToObject(&bes3emcmc,emcmc);
        mcevt->m_emcMcHitCol->Add(emcmc);
    }
    mcevt->m_tofMcHitCol->Clear();
    for(int i=0,total=buf->m_tofmchitcol_size();i<total;i++){
        const bes3::TTofMc &bes3tofmc=buf->m_tofmchitcol(i);
        TTofMc *tofmc=new TTofMc;
        bufferToObject(&bes3tofmc,tofmc);
        mcevt->m_tofMcHitCol->Add(tofmc);
    }
    mcevt->m_mucMcHitCol->Clear();
    for(int i=0,total=buf->m_mucmchitcol_size();i<total;i++){
        const bes3::TMucMc &bes3mucmc=buf->m_mucmchitcol(i);
        TMucMc *mucmc=new TMucMc;
        bufferToObject(&bes3mucmc,mucmc);
        mcevt->m_mucMcHitCol->Add(mucmc);
    }
    mcevt->m_mcParticleCol->Clear();
    for(int i=0,total=buf->m_mcparticlecol_size();i<total;i++){
        const bes3::TMcParticle &bes3mcp=buf->m_mcparticlecol(i);
        TMcParticle *mcp=new TMcParticle;
        bufferToObject(&bes3mcp,mcp);
        mcevt->m_mcParticleCol->Add(mcp);
    }
}

void ObjectHook::bufferToObject(const bes3::TMdcDigi *buf, TMdcDigi *mdc)
{
    const bes3::TRawData &rawdata=buf->m_base();
    bufferToObject(&rawdata,mdc);
    mdc->m_overflow=buf->m_overflow();
}

void ObjectHook::bufferToObject(const bes3::TEmcDigi *buf, TEmcDigi *emc)
{
    const bes3::TRawData &rawdata=buf->m_base();
    bufferToObject(&rawdata,emc);
    emc->m_measure=buf->m_measure();
}

void ObjectHook::bufferToObject(const bes3::TTofDigi *buf, TTofDigi *tof)
{
    const bes3::TRawData &rawdata=buf->m_base();
    bufferToObject(&rawdata,tof);
    tof->m_overflow=buf->m_overflow();
}

void ObjectHook::bufferToObject(const bes3::TLumiDigi *buf, TLumiDigi *lumi)
{
    const bes3::TRawData &rawdata=buf->m_base();
    bufferToObject(&rawdata,lumi);
    lumi->m_overflow=buf->m_overflow();
}

void ObjectHook::bufferToObject(const bes3::TEmcTrack *buf, TEmcTrack *emctrack)
{
    emctrack->m_trackId=buf->m_trackid();
    emctrack->m_numHits=buf->m_numhits();
    emctrack->m_status=buf->m_status();
    emctrack->m_cellId=buf->m_cellid();
    emctrack->m_module=buf->m_module();
    emctrack->m_x=buf->m_x();
    emctrack->m_y=buf->m_y();
    emctrack->m_z=buf->m_z();
    emctrack->m_dTheta=buf->m_dtheta();
    emctrack->m_dPhi=buf->m_dphi();
    emctrack->m_energy=buf->m_energy();
    emctrack->m_dE=buf->m_de();
    emctrack->m_eSeed=buf->m_eseed();
    emctrack->m_e3x3=buf->m_e3x3();
    emctrack->m_e5x5=buf->m_e5x5();
    emctrack->m_time=buf->m_time();
    emctrack->m_secondMoment=buf->m_secondmoment();
    emctrack->m_latMoment=buf->m_latmoment();
    emctrack->m_a20Moment=buf->m_a20moment();
    emctrack->m_a42Moment=buf->m_a42moment();
    for(int i=0,total=buf->m_err_size();i<total;i++){
        emctrack->m_err[i]=buf->m_err(i);
    }
}

void ObjectHook::bufferToObject(const bes3::TMdcTrack *buf, TMdcTrack *mdctrack)
{
    mdctrack->m_trackId=buf->m_trackid();
    for(int i=0;i<15;++i){
        if (i<5){
            mdctrack->m_helix[i]=buf->m_helix(i);
        }
        mdctrack->m_err[i]=buf->m_err(i);
    }
    mdctrack->m_stat=buf->m_stat();
    mdctrack->m_chi2=buf->m_chi2();
    mdctrack->m_ndof=buf->m_ndof();
    mdctrack->m_nster=buf->m_nster();
    mdctrack->m_firstLayer=buf->m_firstlayer();
    mdctrack->m_lastLayer=buf->m_lastlayer();
}

void ObjectHook::bufferToObject(const bes3::TMucTrack *buf, TMucTrack *muctrack)
{
    muctrack->m_trackId=buf->m_trackid();
    muctrack->m_id=buf->m_id();
    muctrack->m_status=buf->m_status();
    muctrack->m_type=buf->m_type();
    muctrack->m_startPart=buf->m_startpart();
    muctrack->m_endPart=buf->m_endpart();
    muctrack->m_brLastLayer=buf->m_brlastlayer();
    muctrack->m_ecLastLayer=buf->m_eclastlayer();
    muctrack->m_numHits=buf->m_numhits();
    muctrack->m_numLayers=buf->m_numlayers();
    muctrack->m_maxHitsInLayer=buf->m_maxhitsinlayer();
    muctrack->m_depth=buf->m_depth();
    muctrack->m_chi2=buf->m_chi2();
    muctrack->m_dof=buf->m_dof();
    muctrack->m_rms=buf->m_rms();
    muctrack->m_xPos=buf->m_xpos();
    muctrack->m_yPos=buf->m_ypos();
    muctrack->m_zPos=buf->m_zpos();
    muctrack->m_xPosSigma=buf->m_xpossigma();
    muctrack->m_yPosSigma=buf->m_ypossigma();
    muctrack->m_zPosSigma=buf->m_zpossigma();
    muctrack->m_px=buf->m_px();
    muctrack->m_py=buf->m_py();
    muctrack->m_pz=buf->m_pz();
    muctrack->m_distance=buf->m_distance();
    muctrack->m_deltaPhi=buf->m_deltaphi();
}

void ObjectHook::bufferToObject(const bes3::TRawData *buf, TRawData *rawdata)
{
    rawdata->m_intId=buf->m_intid();
    rawdata->m_timeChannel=buf->m_timechannel();
    rawdata->m_chargeChannel=buf->m_chargechannel();
    rawdata->m_trackIndex=buf->m_trackindex();
}

void ObjectHook::bufferToObject(const bes3::TMucDigi *buf, TMucDigi *muc)
{
    const bes3::TRawData &rawdata=buf->m_base();
    bufferToObject(&rawdata,muc);
}

void ObjectHook::bufferToObject(const bes3::TTag *buf, TTag *tag)
{
    tag->fRun=buf->frun();
    tag->fRec=buf->frec();
    tag->fNcharge=buf->fncharge();
    tag->fNneu=buf->fnneu();
    tag->fNtot=buf->fntot();
    tag->fNgam=buf->fngam();
    tag->fNgch_p=buf->fngch_p();
    tag->fNgch_m=buf->fngch_m();
    tag->fNpion_p=buf->fnpion_p();
    tag->fNpion_m=buf->fnpion_m();
    tag->fNkaon_p=buf->fnkaon_p();
    tag->fNkaon_m=buf->fnkaon_m();
    tag->fNproton_p=buf->fnproton_p();
    tag->fNproton_m=buf->fnproton_m();
    tag->fEvis=buf->fevis();
}

void ObjectHook::bufferToObject(const bes3::TEvtRecTrack *buf, TEvtRecTrack *recTrack)
{
    recTrack->m_trackId=buf->m_trackid();
    recTrack->m_partId=buf->m_partid();
    recTrack->m_quality=buf->m_quality();
    recTrack->m_mdcTrackId=buf->m_mdctrackid();
    recTrack->m_mdcKalTrackId=buf->m_mdckaltrackid();
    recTrack->m_mdcDedxId=buf->m_mdcdedxid();
    recTrack->m_extTrackId=buf->m_exttrackid();
    recTrack->m_emcShowerId=buf->m_emcshowerid();
    recTrack->m_mucTrackId=buf->m_muctrackid();
    recTrack->m_tofTrackIds.clear();
    for(int i=0,total=buf->m_toftrackids_size();i<total;i++){
        recTrack->m_tofTrackIds.push_back(buf->m_toftrackids(i));
    }
}

void ObjectHook::bufferToObject(const bes3::TEvtRecEvent *buf, TEvtRecEvent *recevt)
{
    recevt->m_tottks=buf->m_tottks();
    recevt->m_nchrg=buf->m_nchrg();
    recevt->m_nneu=buf->m_nneu();
    recevt->m_nvee=buf->m_nvee();
    recevt->m_npi0=buf->m_npi0();
    recevt->m_neta=buf->m_neta();
    recevt->m_ndtag=buf->m_ndtag();
}

void ObjectHook::bufferToObject(const bes3::TEvtRecPrimaryVertex *buf,
                                TEvtRecPrimaryVertex *vtx)
{
    vtx->m_isValid=buf->m_isvalid();
    vtx->m_nTracks=buf->m_ntracks();
    vtx->m_trackIdList.clear();
    for(int i=0,total=buf->m_trackidlist_size();i<total;i++){
        vtx->m_trackIdList.push_back(buf->m_trackidlist(i));
    }
    vtx->m_chi2=buf->m_chi2();
    vtx->m_ndof=buf->m_ndof();
    vtx->m_fitMethod=buf->m_fitmethod();
    for(int i=0;i<6;i++){
        if (i<3){
            vtx->m_vtx[i]=buf->m_vtx(i);
        }
        vtx->m_Evtx[i]=buf->m_evtx(i);
    }
}

void ObjectHook::bufferToObject(const bes3::TEvtRecEtaToGG *buf, TEvtRecEtaToGG *eta)
{
    eta->m_unconMass=buf->m_unconmass();
    eta->m_chisq=buf->m_chisq();
    eta->m_hiPx=buf->m_hipx();
    eta->m_hiPy=buf->m_hipy();
    eta->m_hiPz=buf->m_hipz();
    eta->m_hiPe=buf->m_hipe();
    eta->m_loPx=buf->m_lopx();
    eta->m_loPy=buf->m_lopy();
    eta->m_loPz=buf->m_lopz();
    eta->m_loPe=buf->m_lope();
    eta->m_hiEnGamma=buf->m_hiengamma();
    eta->m_loEnGamma=buf->m_loengamma();
}

void ObjectHook::objectToBuffer(const TEvtHeader *evtheader, bes3::TEvtHeader *buf)
{
    buf->set_m_eventid(evtheader->m_eventId);
    buf->set_m_runid(evtheader->m_runId);
    buf->set_m_time(evtheader->m_time);
    buf->set_m_eventtag(evtheader->m_eventTag);
    buf->set_m_flag1(evtheader->m_flag1);
    buf->set_m_flag2(evtheader->m_flag2);
}

void ObjectHook::objectToBuffer(const TEvtRecObject *evtrecobj,
                                bes3::TEvtRecObject *buf)
{
    buf->Clear();
    bes3::TEvtRecEvent *evtrecevent=buf->mutable_m_evtrecevent();
    objectToBuffer(evtrecobj->m_evtRecEvent,evtrecevent);
    int i,total;
    for (i=0,total=evtrecobj->m_evtRecTrackCol->GetEntries();i<total;i++){
        TEvtRecTrack *track=dynamic_cast<TEvtRecTrack*>(evtrecobj->m_evtRecTrackCol->At(i));
        bes3::TEvtRecTrack *bes3track=buf->add_m_evtrectrackcol();
        objectToBuffer(track,bes3track);
    }
    bes3::TEvtRecPrimaryVertex *primaryvtx=buf->mutable_m_evtrecprimaryvertex();
    objectToBuffer(evtrecobj->m_evtRecPrimaryVertex,primaryvtx);
    for(i=0,total=evtrecobj->m_evtRecVeeVertexCol->GetEntries();i<total;i++){
        TEvtRecVeeVertex *veevtx=
                dynamic_cast<TEvtRecVeeVertex*>(evtrecobj->m_evtRecVeeVertexCol->At(i));
        bes3::TEvtRecVeeVertex *bes3veevtx=buf->add_m_evtrecveevertexcol();
        objectToBuffer(veevtx,bes3veevtx);
    }
    for(i=0,total=evtrecobj->m_evtRecPi0Col->GetEntries();i<total;i++){
        TEvtRecPi0 *pi0=dynamic_cast<TEvtRecPi0*>(evtrecobj->m_evtRecPi0Col->At(i));
        bes3::TEvtRecPi0 *bes3pi0=buf->add_m_evtrecpi0col();
        objectToBuffer(pi0,bes3pi0);
    }
    for(i=0,total=evtrecobj->m_evtRecEtaToGGCol->GetEntries();i<total;i++){
        TEvtRecEtaToGG *eta=dynamic_cast<TEvtRecEtaToGG*>(evtrecobj->m_evtRecEtaToGGCol->At(i));
        bes3::TEvtRecEtaToGG *bes3eta=buf->add_m_evtrecetatoggcol();
        objectToBuffer(eta,bes3eta);
    }
    for(i=0,total=evtrecobj->m_evtRecDTagCol->GetEntries();i<total;i++){
        TEvtRecDTag *dtag=dynamic_cast<TEvtRecDTag*>(evtrecobj->m_evtRecDTagCol->At(i));
        bes3::TEvtRecDTag *bes3dtag=buf->add_m_evtrecdtagcol();
        objectToBuffer(dtag,bes3dtag);
    }
}

void ObjectHook::objectToBuffer(const TDstEvent *dst, bes3::TDstEvent *buf)
{
    buf->Clear();
    int i,total;
    for(i=0,total=dst->m_emcTrackCol->GetEntries();i<total;i++){
        TEmcTrack *emc=dynamic_cast<TEmcTrack*>(dst->m_emcTrackCol->At(i));
        bes3::TEmcTrack *track=buf->add_m_emctrackcol();
        objectToBuffer(emc,track);
    }
    for(i=0,total=dst->m_mdcTrackCol->GetEntries();i<total;i++){
        TMdcTrack *mdc=dynamic_cast<TMdcTrack*>(dst->m_mdcTrackCol->At(i));
        bes3::TMdcTrack *track=buf->add_m_mdctrackcol();
        objectToBuffer(mdc,track);
    }
    for(i=0,total=dst->m_tofTrackCol->GetEntries();i<total;i++){
        TTofTrack *tof=dynamic_cast<TTofTrack*>(dst->m_tofTrackCol->At(i));
        bes3::TTofTrack *track=buf->add_m_toftrackcol();
        objectToBuffer(tof,track);
    }
    for(i=0,total=dst->m_mucTrackCol->GetEntries();i<total;i++){
        TMucTrack *muc=dynamic_cast<TMucTrack*>(dst->m_mucTrackCol->At(i));
        bes3::TMucTrack *track=buf->add_m_muctrackcol();
        objectToBuffer(muc,track);
    }
    for(i=0,total=dst->m_mdcDedxCol->GetEntries();i<total;i++){
        TMdcDedx *dedx=dynamic_cast<TMdcDedx*>(dst->m_mdcDedxCol->At(i));
        bes3::TMdcDedx *bes3dedx=buf->add_m_mdcdedxcol();
        objectToBuffer(dedx,bes3dedx);
    }
    for(i=0,total=dst->m_extTrackCol->GetEntries();i<total;i++){
        TExtTrack *ext=dynamic_cast<TExtTrack*>(dst->m_extTrackCol->At(i));
        bes3::TExtTrack *track=buf->add_m_exttrackcol();
        objectToBuffer(ext,track);
    }
    for(i=0,total=dst->m_mdcKalTrackCol->GetEntries();i<total;i++){
        TMdcKalTrack *mdckal=dynamic_cast<TMdcKalTrack*>(dst->m_mdcKalTrackCol->At(i));
        bes3::TMdcKalTrack *track=buf->add_m_mdckaltrackcol();
        objectToBuffer(mdckal,track);
    }
}

void ObjectHook::objectToBuffer(const TEvtRecEvent *evtrecevent, bes3::TEvtRecEvent *buf)
{
    buf->set_m_tottks(evtrecevent->m_tottks);
    buf->set_m_nchrg(evtrecevent->m_nchrg);
    buf->set_m_nneu(evtrecevent->m_nneu);
    buf->set_m_nvee(evtrecevent->m_nvee);
    buf->set_m_npi0(evtrecevent->m_npi0);
    buf->set_m_neta(evtrecevent->m_neta);
    buf->set_m_ndtag(evtrecevent->m_ndtag);
}

void ObjectHook::objectToBuffer(const TEvtRecPrimaryVertex *vtx,
                                bes3::TEvtRecPrimaryVertex *buf)
{
    buf->set_m_isvalid(vtx->m_isValid);
    buf->set_m_ntracks(vtx->m_nTracks);
    buf->clear_m_trackidlist();
    for(size_t i=0;i<vtx->m_trackIdList.size();i++){
        buf->add_m_trackidlist(vtx->m_trackIdList.at(i));
    }
    buf->set_m_chi2(vtx->m_chi2);
    buf->set_m_ndof(vtx->m_ndof);
    buf->set_m_fitmethod(vtx->m_fitMethod);
    buf->clear_m_vtx();
    buf->clear_m_evtx();
    for (int i=0;i<6;++i){
        if (i<3){
            buf->add_m_vtx(vtx->m_vtx[i]);
        }
        buf->add_m_evtx(vtx->m_Evtx[i]);
    }
}

void ObjectHook::objectToBuffer(const TEvtRecVeeVertex *vtx,
                                bes3::TEvtRecVeeVertex *buf)
{
    buf->Clear();
    buf->set_m_vertexid(vtx->m_vertexId);
    buf->set_m_vertextype(vtx->m_vertexType);
    buf->set_m_chi2(vtx->m_chi2);
    buf->set_m_ndof(vtx->m_ndof);
    buf->set_m_mass(vtx->m_mass);
    for(int i=0;i<28;i++){
        if (i<7){
            buf->add_m_w(vtx->m_w[i]);
        }
        buf->add_m_ew(vtx->m_Ew[i]);
    }
    buf->set_m_ncharge(vtx->m_nCharge);
    buf->set_m_ntracks(vtx->m_nTracks);
    for(int i=0;i<2;i++){
        buf->add_m_pair(vtx->m_pair[i]);
        buf->add_m_daughter(vtx->m_daughter[i]);
    }
}

void ObjectHook::objectToBuffer(const TEvtRecPi0 *pi0, bes3::TEvtRecPi0 *buf)
{
    buf->set_m_unconmass(pi0->m_unconMass);
    buf->set_m_chisq(pi0->m_chisq);
    buf->set_m_hipx(pi0->m_hiPx);
    buf->set_m_hipy(pi0->m_hiPy);
    buf->set_m_hipz(pi0->m_hiPz);
    buf->set_m_hipe(pi0->m_hiPe);
    buf->set_m_lopx(pi0->m_loPx);
    buf->set_m_lopy(pi0->m_loPy);
    buf->set_m_lopz(pi0->m_loPz);
    buf->set_m_lope(pi0->m_loPe);
    buf->set_m_hiengamma(pi0->m_hiEnGamma);
    buf->set_m_loengamma(pi0->m_loEnGamma);
}

void ObjectHook::objectToBuffer(const TEvtRecEtaToGG *eta, bes3::TEvtRecEtaToGG *buf)
{
    buf->set_m_unconmass(eta->m_unconMass);
    buf->set_m_chisq(eta->m_chisq);
    buf->set_m_hipx(eta->m_hiPx);
    buf->set_m_hipy(eta->m_hiPy);
    buf->set_m_hipz(eta->m_hiPz);
    buf->set_m_hipe(eta->m_hiPe);
    buf->set_m_lopx(eta->m_loPx);
    buf->set_m_lopy(eta->m_loPy);
    buf->set_m_lopz(eta->m_loPz);
    buf->set_m_lope(eta->m_loPe);
    buf->set_m_hiengamma(eta->m_hiEnGamma);
    buf->set_m_loengamma(eta->m_loEnGamma);
}

void ObjectHook::objectToBuffer(const TEvtRecDTag *dtag, bes3::TEvtRecDTag *buf)
{
    buf->Clear();
    buf->set_m_decaymode(dtag->m_decayMode);
    buf->set_m_type(dtag->m_type);
    buf->set_m_beame(dtag->m_beamE);
    buf->set_m_mass(dtag->m_mass);
    buf->set_m_mbc(dtag->m_mBC);
    buf->set_m_deltae(dtag->m_deltaE);
    buf->set_m_charge(dtag->m_charge);
    buf->set_m_charm(dtag->m_charm);
    buf->set_m_numofchildren(dtag->m_numOfChildren);
    buf->set_m_px(dtag->m_px);
    buf->set_m_py(dtag->m_py);
    buf->set_m_pz(dtag->m_pz);
    buf->set_m_pe(dtag->m_pe);
    for(size_t i=0;i<dtag->m_tracks.size();i++){
        buf->add_m_tracks(dtag->m_tracks.at(i));
    }
    for (size_t i=0;i<dtag->m_showers.size();++i){
        buf->add_m_showers(dtag->m_showers.at(i));
    }
    for(size_t i=0;i<dtag->m_otherTracks.size();i++){
        buf->add_m_othertracks(dtag->m_otherTracks.at(i));
    }
    for(size_t i=0;i<dtag->m_otherShowers.size();i++){
        buf->add_m_othershowers(dtag->m_otherShowers.at(i));
    }
    for(size_t i=0;i<dtag->m_pionId.size();++i){
        buf->add_m_pionid(dtag->m_pionId.at(i));
    }
    for(size_t i=0;i<dtag->m_kaonId.size();i++){
        buf->add_m_kaonid(dtag->m_kaonId.at(i));
    }
}

void ObjectHook::objectToBuffer(const THltEvent *hltevent, bes3::THltEvent *buf)
{
    buf->set_m_frommc(hltevent->m_fromMc);
    buf->clear_m_hltrawcol();
    for (int i=0,total=hltevent->m_hltRawCol->GetEntries();i<total;i++){
        THltRaw *hltraw=dynamic_cast<THltRaw*>(hltevent->m_hltRawCol->At(i));
        bes3::THltRaw *bes3hltraw=buf->add_m_hltrawcol();
        objectToBuffer(hltraw,bes3hltraw);
    }
    bes3::THltInf *hltinf=buf->mutable_m_hltinf();
    objectToBuffer(hltevent->m_hltInf,hltinf);
    bes3::TDstHltInf *dsthltinf=buf->mutable_m_dsthltinf();
    objectToBuffer(hltevent->m_dstHltInf,dsthltinf);
}

void ObjectHook::objectToBuffer(const TTag *ttag, bes3::TTag *buf)
{
    buf->set_frun(ttag->fRun);
    buf->set_frec(ttag->fRec);
    buf->set_fncharge(ttag->fNcharge);
    buf->set_fnneu(ttag->fNneu);
    buf->set_fntot(ttag->fNtot);
    buf->set_fngam(ttag->fNgam);
    buf->set_fngch_p(ttag->fNgch_p);
    buf->set_fngch_m(ttag->fNgch_m);
    buf->set_fnpion_p(ttag->fNpion_p);
    buf->set_fnpion_m(ttag->fNpion_m);
    buf->set_fnkaon_p(ttag->fNkaon_p);
    buf->set_fnkaon_m(ttag->fNkaon_m);
    buf->set_fnproton_p(ttag->fNproton_p);
    buf->set_fnproton_m(ttag->fNproton_m);
    buf->set_fevis(ttag->fEvis);
}

void ObjectHook::objectToBuffer(const TEvtRecTrack *evtrectrack, bes3::TEvtRecTrack *buf)
{
    buf->set_m_trackid(evtrectrack->m_trackId);
    buf->set_m_partid(evtrectrack->m_partId);
    buf->set_m_quality(evtrectrack->m_quality);
    buf->set_m_mdctrackid(evtrectrack->m_mdcTrackId);
    buf->set_m_mdckaltrackid(evtrectrack->m_mdcKalTrackId);
    buf->set_m_mdcdedxid(evtrectrack->m_mdcDedxId);
    buf->set_m_exttrackid(evtrectrack->m_extTrackId);
    buf->set_m_emcshowerid(evtrectrack->m_emcShowerId);
    buf->set_m_muctrackid(evtrectrack->m_mucTrackId);
    buf->clear_m_toftrackids();
    for(size_t i=0;i<evtrectrack->m_tofTrackIds.size();i++){
        buf->add_m_toftrackids(evtrectrack->m_tofTrackIds.at(i));
    }
}

void ObjectHook::objectToBuffer(const THltInf *hltinf, bes3::THltInf *buf)
{
    buf->set_m_evttype(hltinf->m_evtType);
    buf->set_m_algprocess(hltinf->m_algProcess);
    buf->set_m_critable(hltinf->m_criTable);
    buf->set_m_vernumber(hltinf->m_verNumber);
    buf->set_m_etotal(hltinf->m_eTotal);
    buf->set_m_subnumber(hltinf->m_subNumber);
    buf->set_m_connumber(hltinf->m_conNumber);
    buf->clear_m_mdcdata();
    for (size_t i=0;i<hltinf->m_mdcData.size();++i){
        buf->add_m_mdcdata(hltinf->m_mdcData.at(i));
    }
    buf->clear_m_tofdata();
    for(size_t i=0;i<hltinf->m_tofData.size();i++){
        buf->add_m_tofdata(hltinf->m_tofData.at(i));
    }
    buf->clear_m_emcdata();
    for (size_t i=0;i<hltinf->m_emcData.size();i++){
        buf->add_m_emcdata(hltinf->m_emcData.at(i));
    }
    buf->clear_m_mucdata();
    for(size_t i=0;i<hltinf->m_mucData.size();i++){
        buf->add_m_mucdata(hltinf->m_mucData.at(i));
    }
    buf->clear_m_condata();
    for(size_t i=0;i<hltinf->m_conData.size();i++){
        buf->add_m_condata(hltinf->m_conData.at(i));
    }
}

void ObjectHook::objectToBuffer(const TDstHltInf *dsthltinf, bes3::TDstHltInf *buf)
{
    buf->set_m_evttype(dsthltinf->m_evtType);
    buf->set_m_algprocess(dsthltinf->m_algProcess);
    buf->set_m_critable(dsthltinf->m_criTable);
    buf->set_m_vernumber(dsthltinf->m_verNumber);
    buf->set_m_etotal(dsthltinf->m_eTotal);
    buf->set_m_subnumber(dsthltinf->m_subNumber);
    buf->set_m_connumber(dsthltinf->m_conNumber);
}

void ObjectHook::objectToBuffer(const THltRaw *hltraw, bes3::THltRaw *buf)
{
    bes3::TRawData *base=buf->mutable_base();
    objectToBuffer(hltraw,base);
}

void ObjectHook::objectToBuffer(const TMdcMc *mdcmc, bes3::TMdcMc *buf)
{
    buf->set_m_id(mdcmc->m_id);
    buf->set_m_trackindex(mdcmc->m_trackIndex);
    buf->set_m_xposition(mdcmc->m_xPosition);
    buf->set_m_yposition(mdcmc->m_yPosition);
    buf->set_m_zposition(mdcmc->m_zPosition);
    buf->set_m_driftdistance(mdcmc->m_driftDistance);
    buf->set_m_depositenergy(mdcmc->m_depositEnergy);
    buf->set_m_posflag(mdcmc->m_posFlag);
}

void ObjectHook::objectToBuffer(const TEmcMc *emcmc, bes3::TEmcMc *buf)
{
    buf->set_m_hitemc(emcmc->m_hitEmc);
    buf->set_m_pdgcode(emcmc->m_PDGCode);
    buf->set_m_pdgcharge(emcmc->m_PDGCharge);
    buf->set_m_time(emcmc->m_time);
    buf->set_m_id(emcmc->m_id);
    buf->set_m_trackindex(emcmc->m_trackIndex);
    buf->set_m_xposition(emcmc->m_xPosition);
    buf->set_m_yposition(emcmc->m_yPosition);
    buf->set_m_zposition(emcmc->m_zPosition);
    buf->set_m_px(emcmc->m_px);
    buf->set_m_py(emcmc->m_py);
    buf->set_m_pz(emcmc->m_pz);
    buf->set_m_depositenergy(emcmc->m_depositEnergy);
    buf->clear_m_hitmap();
    std::map<int,double>::const_iterator iter=emcmc->m_hitMap.begin();
    for(;iter!=emcmc->m_hitMap.end();++iter){
        bes3::TEmcMc_HitMapEntry *entry=buf->add_m_hitmap();
        entry->set_key(iter->first);
        entry->set_value(iter->second);
    }
}

void ObjectHook::objectToBuffer(const TTofMc *tofmc, bes3::TTofMc *buf)
{
    buf->set_m_id(tofmc->m_id);
    buf->set_m_trackindex(tofmc->m_trackIndex);
    buf->set_m_xposition(tofmc->m_xPosition);
    buf->set_m_yposition(tofmc->m_yPosition);
    buf->set_m_zposition(tofmc->m_zPosition);
    buf->set_m_px(tofmc->m_px);
    buf->set_m_py(tofmc->m_py);
    buf->set_m_pz(tofmc->m_pz);
    buf->set_m_tracklength(tofmc->m_trackLength);
    buf->set_m_flighttime(tofmc->m_flightTime);
}

void ObjectHook::objectToBuffer(const TMucMc *mucmc, bes3::TMucMc *buf)
{
    buf->set_m_id(mucmc->m_id);
    buf->set_m_trackindex(mucmc->m_trackIndex);
    buf->set_m_xposition(mucmc->m_xPosition);
    buf->set_m_yposition(mucmc->m_yPosition);
    buf->set_m_zposition(mucmc->m_zPosition);
    buf->set_m_px(mucmc->m_px);
    buf->set_m_py(mucmc->m_py);
    buf->set_m_pz(mucmc->m_pz);
    buf->set_m_depositenergy(mucmc->m_depositEnergy);
}

void ObjectHook::objectToBuffer(const TMcParticle *mcp, bes3::TMcParticle *buf)
{
    buf->set_m_particleid(mcp->m_particleID);
    buf->set_m_trackindex(mcp->m_trackIndex);
    buf->set_m_vertexindex0(mcp->m_vertexIndex0);
    buf->set_m_vertexindex1(mcp->m_vertexIndex1);
    buf->set_m_statusflags(mcp->m_statusFlags);
    buf->set_m_xinitialposition(mcp->m_xInitialPosition);
    buf->set_m_yinitialposition(mcp->m_yInitialPosition);
    buf->set_m_zinitialposition(mcp->m_zInitialPosition);
    buf->set_m_tinitialposition(mcp->m_tInitialPosition);
    buf->set_m_xfinalposition(mcp->m_xFinalPosition);
    buf->set_m_yfinalposition(mcp->m_yFinalPosition);
    buf->set_m_zfinalposition(mcp->m_zFinalPosition);
    buf->set_m_tfinalposition(mcp->m_tFinalPosition);
    buf->set_m_xinitialmomentum(mcp->m_xInitialMomentum);
    buf->set_m_yinitialmomentum(mcp->m_yInitialMomentum);
    buf->set_m_zinitialmomentum(mcp->m_zInitialMomentum);
    buf->set_m_tinitialmomentum(mcp->m_eInitialMomentum);
    buf->set_m_mother(mcp->m_mother);
    buf->clear_m_daughters();
    for(size_t i=0;i<mcp->m_daughters.size();i++){
        buf->add_m_daughters(mcp->m_daughters.at(i));
    }
}

void ObjectHook::objectToBuffer(const TDigiEvent *digievent, bes3::TDigiEvent *buf)
{
    buf->set_m_frommc(digievent->m_fromMc);
    int i,total;
    buf->clear_m_mdcdigicol();
    for (i=0,total=digievent->m_mdcDigiCol->GetEntries();i<total;++i){
        TMdcDigi *digi=dynamic_cast<TMdcDigi*>(digievent->m_mdcDigiCol->At(i));
        bes3::TMdcDigi* bes3digi=buf->add_m_mdcdigicol();
        objectToBuffer(digi,bes3digi);
    }
    buf->clear_m_emcdigicol();
    for (i=0,total=digievent->m_emcDigiCol->GetEntries();i<total;++i){
        TEmcDigi *digi=dynamic_cast<TEmcDigi*>(digievent->m_emcDigiCol->At(i));
        bes3::TEmcDigi *bes3digi=buf->add_m_emcdigicol();
        objectToBuffer(digi,bes3digi);
    }
    buf->clear_m_tofdigicol();
    for (i=0,total=digievent->m_tofDigiCol->GetEntries();i<total;i++){
        TTofDigi *digi=dynamic_cast<TTofDigi*>(digievent->m_tofDigiCol->At(i));
        bes3::TTofDigi *bes3digi=buf->add_m_tofdigicol();
        objectToBuffer(digi,bes3digi);
    }
    buf->clear_m_mucdigicol();
    for(i=0,total=digievent->m_mucDigiCol->GetEntries();i<total;i++){
        TMucDigi *digi=dynamic_cast<TMucDigi*>(digievent->m_mucDigiCol->At(i));
        bes3::TMucDigi *bes3digi=buf->add_m_mucdigicol();
        objectToBuffer(digi,bes3digi);
    }
    buf->clear_m_lumidigicol();
    for(i=0,total=digievent->m_lumiDigiCol->GetEntries();i<total;i++){
        TLumiDigi *digi=dynamic_cast<TLumiDigi*>(digievent->m_lumiDigiCol->At(i));
        bes3::TLumiDigi *bes3digi=buf->add_m_lumidigicol();
        objectToBuffer(digi,bes3digi);
    }
}

void ObjectHook::objectToBuffer(const TRawData *rawdata, bes3::TRawData *buf)
{
    buf->set_m_intid(rawdata->m_intId);
    buf->set_m_timechannel(rawdata->m_timeChannel);
    buf->set_m_chargechannel(rawdata->m_chargeChannel);
    buf->set_m_trackindex(rawdata->m_trackIndex);
}

void ObjectHook::objectToBuffer(const TMdcDigi *mdcdigi, bes3::TMdcDigi *buf)
{
    bes3::TRawData *base=buf->mutable_m_base();
    objectToBuffer(mdcdigi,base);
    buf->set_m_overflow(mdcdigi->m_overflow);
}

void ObjectHook::objectToBuffer(const TEmcDigi *emcdigi, bes3::TEmcDigi *buf)
{
    bes3::TRawData *base=buf->mutable_m_base();
    objectToBuffer(emcdigi,base);
    buf->set_m_measure(emcdigi->m_measure);
}

void ObjectHook::objectToBuffer(const TTofDigi *tofdigi, bes3::TTofDigi *buf)
{
    bes3::TRawData *base=buf->mutable_m_base();
    objectToBuffer(tofdigi,base);
    buf->set_m_overflow(tofdigi->m_overflow);
}

void ObjectHook::objectToBuffer(const TMucDigi *mucdigi, bes3::TMucDigi *buf)
{
    bes3::TRawData *base=buf->mutable_m_base();
    objectToBuffer(mucdigi,base);
}

void ObjectHook::objectToBuffer(const TLumiDigi *lumidigi, bes3::TLumiDigi *buf)
{
    bes3::TRawData *base=buf->mutable_m_base();
    objectToBuffer(lumidigi,base);
    buf->set_m_overflow(lumidigi->m_overflow);
}

void ObjectHook::objectToBuffer(const TEmcTrack *emctrack, bes3::TEmcTrack *buf)
{
    buf->Clear();
    buf->set_m_trackid(emctrack->m_trackId);
    buf->set_m_numhits(emctrack->m_numHits);
    buf->set_m_status(emctrack->m_status);
    buf->set_m_cellid(emctrack->m_cellId);
    buf->set_m_module(emctrack->m_module);
    buf->set_m_x(emctrack->m_x);
    buf->set_m_y(emctrack->m_y);
    buf->set_m_z(emctrack->m_z);
    buf->set_m_dtheta(emctrack->m_dTheta);
    buf->set_m_dphi(emctrack->m_dPhi);
    buf->set_m_energy(emctrack->m_energy);
    buf->set_m_de(emctrack->m_dE);
    buf->set_m_eseed(emctrack->m_eSeed);
    buf->set_m_e3x3(emctrack->m_e3x3);
    buf->set_m_e5x5(emctrack->m_e5x5);
    buf->set_m_time(emctrack->m_time);
    buf->set_m_secondmoment(emctrack->m_secondMoment);
    buf->set_m_latmoment(emctrack->m_latMoment);
    buf->set_m_a20moment(emctrack->m_a20Moment);
    buf->set_m_a42moment(emctrack->m_a42Moment);
    for(int i=0;i<6;i++){
        buf->add_m_err(emctrack->m_err[i]);
    }
}

void ObjectHook::objectToBuffer(const TMdcTrack *mdctrack, bes3::TMdcTrack *buf)
{
    buf->Clear();
    buf->set_m_trackid(mdctrack->m_trackId);
    for(int i=0;i<15;i++){
        if (i<5){
            buf->add_m_helix(mdctrack->m_helix[i]);
        }
        buf->add_m_err(mdctrack->m_err[i]);
    }
    buf->set_m_stat(mdctrack->m_stat);
    buf->set_m_chi2(mdctrack->m_chi2);
    buf->set_m_ndof(mdctrack->m_ndof);
    buf->set_m_nster(mdctrack->m_nster);
    buf->set_m_firstlayer(mdctrack->m_firstLayer);
    buf->set_m_lastlayer(mdctrack->m_lastLayer);
}

void ObjectHook::objectToBuffer(const TTofTrack *toftrack, bes3::TTofTrack *buf)
{
    buf->Clear();
    buf->set_m_toftrackid(toftrack->m_tofTrackID);
    buf->set_m_trackid(toftrack->m_trackID);
    buf->set_m_tofid(toftrack->m_tofID);
    buf->set_m_status(toftrack->m_status);
    buf->set_m_path(toftrack->m_path);
    buf->set_m_zrhit(toftrack->m_zrhit);
    buf->set_m_ph(toftrack->m_ph);
    buf->set_m_tof(toftrack->m_tof);
    buf->set_m_beta(toftrack->m_beta);
    for(int i=0;i<6;i++){
        if (i<5){
            buf->add_m_texp(toftrack->m_texp[i]);
        }
        buf->add_m_toffset(toftrack->m_toffset[i]);
        buf->add_m_sigma(toftrack->m_sigma[i]);
    }
    buf->set_m_quality(toftrack->m_quality);
    buf->set_m_t0(toftrack->m_t0);
    buf->set_m_errt0(toftrack->m_errt0);
    buf->set_m_errtz(toftrack->m_errz);
    buf->set_m_phi(toftrack->m_phi);
    buf->set_m_errphi(toftrack->m_errphi);
    buf->set_m_energy(toftrack->m_energy);
    buf->set_m_errenergy(toftrack->m_errenergy);
}

void ObjectHook::objectToBuffer(const TMucTrack *muctrack, bes3::TMucTrack *buf)
{
    buf->set_m_trackid(muctrack->m_trackId);
    buf->set_m_id(muctrack->m_id);
    buf->set_m_status(muctrack->m_status);
    buf->set_m_type(muctrack->m_type);
    buf->set_m_startpart(muctrack->m_startPart);
    buf->set_m_endpart(muctrack->m_endPart);
    buf->set_m_brlastlayer(muctrack->m_brLastLayer);
    buf->set_m_eclastlayer(muctrack->m_ecLastLayer);
    buf->set_m_numhits(muctrack->m_numHits);
    buf->set_m_numlayers(muctrack->m_numLayers);
    buf->set_m_maxhitsinlayer(muctrack->m_maxHitsInLayer);
    buf->set_m_depth(muctrack->m_depth);
    buf->set_m_chi2(muctrack->m_chi2);
    buf->set_m_dof(muctrack->m_dof);
    buf->set_m_rms(muctrack->m_rms);
    buf->set_m_xpos(muctrack->m_xPos);
    buf->set_m_ypos(muctrack->m_yPos);
    buf->set_m_zpos(muctrack->m_zPos);
    buf->set_m_xpossigma(muctrack->m_xPosSigma);
    buf->set_m_ypossigma(muctrack->m_yPosSigma);
    buf->set_m_zpossigma(muctrack->m_zPosSigma);
    buf->set_m_px(muctrack->m_px);
    buf->set_m_py(muctrack->m_py);
    buf->set_m_pz(muctrack->m_pz);
    buf->set_m_distance(muctrack->m_distance);
    buf->set_m_deltaphi(muctrack->m_deltaPhi);
}

void ObjectHook::objectToBuffer(const TMdcDedx *dedx, bes3::TMdcDedx *buf)
{
    buf->set_m_trackid(dedx->m_trackId);
    buf->set_m_particleid(dedx->m_particleId);
    buf->set_m_status(dedx->m_status);
    buf->set_m_trunc_alg(dedx->m_trunc_alg);
    buf->set_m_chie(dedx->m_chiE);
    buf->set_m_chimu(dedx->m_chiMu);
    buf->set_m_chipi(dedx->m_chiPi);
    buf->set_m_chik(dedx->m_chiK);
    buf->set_m_chip(dedx->m_chiP);
    buf->set_m_numgoodhits(dedx->m_numGoodHits);
    buf->set_m_numtotalhits(dedx->m_numTotalHits);
    buf->set_m_probph(dedx->m_probPH);
    buf->set_m_normph(dedx->m_normPH);
    buf->set_m_errorph(dedx->m_errorPH);
    buf->set_m_twentyph(dedx->m_twentyPH);
    buf->clear_m_chi();
    for(int i=0;i<5;i++){
        buf->add_m_chi(dedx->m_chi[i]);
    }
}

void ObjectHook::objectToBuffer(const TExtTrack *ext, bes3::TExtTrack *buf)
{
    buf->Clear();
    buf->set_m_trackid(ext->m_trackId);
    buf->set_mytof1positionx(ext->myTof1PositionX);
    buf->set_mytof1positiony(ext->myTof1PositionY);
    buf->set_mytof1positionz(ext->myTof1PositionZ);
    buf->set_mytof1momentumx(ext->myTof1MomentumX);
    buf->set_mytof1momentumy(ext->myTof1MomentumY);
    buf->set_mytof1momentumz(ext->myTof1MomentumZ);
    buf->set_mytof1volumename(ext->myTof1VolumeName.Data());
    buf->set_mytof1volumenumber(ext->myTof1VolumeNumber);
    buf->set_mytof1(ext->myTof1);
    buf->set_mytof1path(ext->myTof1Path);
    buf->set_mytof1possigmaalongz(ext->myTof1PosSigmaAlongZ);
    buf->set_mytof1possigmaalongt(ext->myTof1PosSigmaAlongT);
    buf->set_mytof1possigmaalongx(ext->myTof1PosSigmaAlongX);
    buf->set_mytof1possigmaalongy(ext->myTof1PosSigmaAlongY);
    int i,j;
    for (i=0;i<6;i++){
        for (j=0;j<6;j++){
            buf->add_mytof1errormatrix(ext->myTof1ErrorMatrix[i][j]);
        }
    }
    buf->set_mytof2positionx(ext->myTof2PositionX);
    buf->set_mytof2positiony(ext->myTof2PositionY);
    buf->set_mytof2positionz(ext->myTof2PositionZ);
    buf->set_mytof2momentumx(ext->myTof2MomentumX);
    buf->set_mytof2momentumy(ext->myTof2MomentumY);
    buf->set_mytof2momentumz(ext->myTof2MomentumZ);
    buf->set_mytof2volumename(ext->myTof2VolumeName.Data());
    buf->set_mytof2volumenumber(ext->myTof2VolumeNumber);
    buf->set_mytof2(ext->myTof2);
    buf->set_mytof2path(ext->myTof2Path);
    buf->set_mytof2possigmaalongz(ext->myTof2PosSigmaAlongZ);
    buf->set_mytof2possigmaalongt(ext->myTof2PosSigmaAlongT);
    buf->set_mytof2possigmaalongx(ext->myTof2PosSigmaAlongX);
    buf->set_mytof2possigmaalongy(ext->myTof2PosSigmaAlongY);
    for(i=0;i<6;i++){
        for(j=0;j<6;j++){
            buf->add_mytof2errormatrix(ext->myTof2ErrorMatrix[i][j]);
        }
    }
    buf->set_myemcpositionx(ext->myEmcPositionX);
    buf->set_myemcpositiony(ext->myEmcPositionY);
    buf->set_myemcpositionz(ext->myEmcPositionZ);
    buf->set_myemcmomentumx(ext->myEmcMomentumX);
    buf->set_myemcmomentumy(ext->myEmcMomentumY);
    buf->set_myemcmomentumz(ext->myEmcMomentumZ);
    buf->set_myemcvolumename(ext->myEmcVolumeName.Data());
    buf->set_myemcvolumenumber(ext->myEmcVolumeNumber);
    buf->set_myemcpossigmaalongtheta(ext->myEmcPosSigmaAlongTheta);
    buf->set_myemcpossigmaalongphi(ext->myEmcPosSigmaAlongPhi);
    for(i=0;i<6;i++){
        for(j=0;j<6;j++){
            buf->add_myemcerrormatrix(ext->myEmcErrorMatrix[i][j]);
        }
    }
    buf->set_myemcpath(ext->myEmcPath);
    buf->set_mymucpositionx(ext->myMucPositionX);
    buf->set_mymucpositiony(ext->myMucPositionY);
    buf->set_mymucpositionz(ext->myMucPositionZ);
    buf->set_mymucmomentumx(ext->myMucMomentumX);
    buf->set_mymucmomentumy(ext->myMucMomentumY);
    buf->set_mymucmomentumz(ext->myMucMomentumZ);
    buf->set_mymucvolumename(ext->myMucVolumeName.Data());
    buf->set_mymucvolumenumber(ext->myMucVolumeNumber);
    buf->set_mymucpossigmaalongz(ext->myMucPosSigmaAlongZ);
    buf->set_mymucpossigmaalongt(ext->myMucPosSigmaAlongT);
    buf->set_mymucpossigmaalongx(ext->myMucPosSigmaAlongX);
    buf->set_mymucpossigmaalongy(ext->myMucPosSigmaAlongY);
    for(i=0;i<6;i++){
        for(j=0;j<6;j++){
            buf->add_mymucerrormatrix(ext->myMucErrorMatrix[i][j]);
        }
    }
}

void ObjectHook::objectToBuffer(const TMdcKalTrack *mdckal, bes3::TMdcKalTrack *buf)
{
    buf->Clear();
    buf->set_m_trackid(mdckal->m_trackId);
    for(int i=0;i<5;i++){
        buf->add_m_stat(mdckal->m_stat[i]);
        buf->add_m_stat2(mdckal->m_stat2[i]);
        buf->add_m_chisq(mdckal->m_chisq[i]);
        buf->add_m_ndf(mdckal->m_ndf[i]);
        buf->add_m_nster(mdckal->m_nster[i]);
        buf->add_m_firstlayer(mdckal->m_firstLayer[i]);
        buf->add_m_lastlayer(mdckal->m_lastLayer[i]);
        buf->add_m_zhelix(mdckal->m_zhelix[i]);
        buf->add_m_zhelix_e(mdckal->m_zhelix_e[i]);
        buf->add_m_zhelix_mu(mdckal->m_zhelix_mu[i]);
        buf->add_m_zhelix_k(mdckal->m_zhelix_k[i]);
        buf->add_m_zhelix_p(mdckal->m_zhelix_p[i]);
        buf->add_m_fhelix(mdckal->m_fhelix[i]);
        buf->add_m_fhelix_e(mdckal->m_fhelix_e[i]);
        buf->add_m_fhelix_mu(mdckal->m_fhelix_mu[i]);
        buf->add_m_fhelix_k(mdckal->m_fhelix_k[i]);
        buf->add_m_fhelix_p(mdckal->m_fhelix_p[i]);
        for(int j=0;j<5;++j){
            buf->add_m_zerror(mdckal->m_zerror[i][j]);
            buf->add_m_zerror_e(mdckal->m_zerror_e[i][j]);
            buf->add_m_zerror_mu(mdckal->m_zerror_mu[i][j]);
            buf->add_m_zerror_k(mdckal->m_zerror_k[i][j]);
            buf->add_m_zerror_p(mdckal->m_zerror_p[i][j]);
            buf->add_m_ferror(mdckal->m_ferror[i][j]);
            buf->add_m_ferror_e(mdckal->m_ferror_e[i][j]);
            buf->add_m_ferror_mu(mdckal->m_ferror_mu[i][j]);
            buf->add_m_ferror_k(mdckal->m_ferror_k[i][j]);
            buf->add_m_ferror_p(mdckal->m_ferror_p[i][j]);
        }
    }
}

void ObjectHook::objectToBuffer(const TTrigEvent *trigevent, bes3::TTrigEvent *buf)
{
    buf->set_m_frommc(trigevent->m_fromMc);
    bes3::TTrigData *trigdata=buf->mutable_m_trigdata();
    objectToBuffer(trigevent->m_trigData,trigdata);
}

void ObjectHook::objectToBuffer(const TMcEvent *mcevt, bes3::TMcEvent *buf)
{
    int i,total;
    buf->Clear();
    for(i=0,total=mcevt->m_mdcMcHitCol->GetEntries();i<total;i++){
        TMdcMc *mdcmc=dynamic_cast<TMdcMc*>(mcevt->m_mdcMcHitCol->At(i));
        bes3::TMdcMc *bes3mdcmc=buf->add_m_mdcmchitcol();
        objectToBuffer(mdcmc,bes3mdcmc);
    }
    for(i=0,total=mcevt->m_emcMcHitCol->GetEntries();i<total;i++){
        TEmcMc *emcmc=dynamic_cast<TEmcMc*>(mcevt->m_emcMcHitCol->At(i));
        bes3::TEmcMc *bes3emcmc=buf->add_m_emcmchitcol();
        objectToBuffer(emcmc,bes3emcmc);
    }
    for(i=0,total=mcevt->m_tofMcHitCol->GetEntries();i<total;i++){
        TTofMc *tofmc=dynamic_cast<TTofMc*>(mcevt->m_tofMcHitCol->At(i));
        bes3::TTofMc *bes3tofmc=buf->add_m_tofmchitcol();
        objectToBuffer(tofmc,bes3tofmc);
    }
    for(i=0,total=mcevt->m_mucMcHitCol->GetEntries();i<total;i++){
        TMucMc *mucmc=dynamic_cast<TMucMc*>(mcevt->m_mucMcHitCol->At(i));
        bes3::TMucMc *bes3mucmc=buf->add_m_mucmchitcol();
        objectToBuffer(mucmc,bes3mucmc);
    }
    for(i=0,total=mcevt->m_mcParticleCol->GetEntries();i<total;i++){
        TMcParticle *mcp=dynamic_cast<TMcParticle*>(mcevt->m_mcParticleCol->At(i));
        bes3::TMcParticle *bes3mcp=buf->add_m_mcparticlecol();
        objectToBuffer(mcp,bes3mcp);
    }
}

void ObjectHook::objectToBuffer(const TTrigData *trigdata, bes3::TTrigData *buf)
{
    buf->set_m_prescale(trigdata->m_preScale);
    buf->clear_m_trigcond();
    buf->clear_m_trigchan();
    for (int i=0;i<48;++i){
        buf->add_m_trigcond(trigdata->m_trigcond[i]);
        if (i<16){
            buf->add_m_trigchan(trigdata->m_trigChan[i]);
        }
    }
    buf->set_m_timewindow(trigdata->m_timeWindow);
    buf->set_m_timing(trigdata->m_Timing);
}

void ObjectHook::bufferToObject(const bes3::TEvtHeader *buf, TEvtHeader *evtHeader)
{
    evtHeader->m_eventId=buf->m_eventid();
    evtHeader->m_runId=buf->m_runid();
    evtHeader->m_time=buf->m_time();
    evtHeader->m_eventTag=buf->m_eventtag();
    evtHeader->m_flag1=buf->m_flag1();
    evtHeader->m_flag2=buf->m_flag2();
}

void ObjectHook::bufferToObject(const bes3::TEvtRecObject *buf, TEvtRecObject *recobj)
{
    const bes3::TEvtRecEvent &recevent=buf->m_evtrecevent();
    bufferToObject(&recevent,recobj->m_evtRecEvent);
    recobj->m_evtRecTrackCol->Clear();
    for(int i=0,total=buf->m_evtrectrackcol_size();i<total;i++){
        const bes3::TEvtRecTrack &bes3track=buf->m_evtrectrackcol(i);
        TEvtRecTrack *track=new TEvtRecTrack;
        bufferToObject(&bes3track,track);
        recobj->m_evtRecTrackCol->Add(track);
    }
    const bes3::TEvtRecPrimaryVertex &bes3vtx=buf->m_evtrecprimaryvertex();
    bufferToObject(&bes3vtx,recobj->m_evtRecPrimaryVertex);
    recobj->m_evtRecVeeVertexCol->Clear();
    for(int i=0,total=buf->m_evtrecveevertexcol_size();i<total;i++){
        const bes3::TEvtRecVeeVertex &bes3veevtx=buf->m_evtrecveevertexcol(i);
        TEvtRecVeeVertex *vtx=new TEvtRecVeeVertex;
        bufferToObject(&bes3veevtx,vtx);
        recobj->m_evtRecVeeVertexCol->Add(vtx);
    }
    recobj->m_evtRecPi0Col->Clear();
    for(int i=0,total=buf->m_evtrecpi0col_size();i<total;i++){
        const bes3::TEvtRecPi0 &bes3pi0=buf->m_evtrecpi0col(i);
        TEvtRecPi0 *pi0=new TEvtRecPi0;
        bufferToObject(&bes3pi0,pi0);
        recobj->m_evtRecPi0Col->Add(pi0);
    }
    recobj->m_evtRecEtaToGGCol->Clear();
    for(int i=0,total=buf->m_evtrecetatoggcol_size();i<total;i++){
        const bes3::TEvtRecEtaToGG &bes3eta=buf->m_evtrecetatoggcol(i);
        TEvtRecEtaToGG *eta=new TEvtRecEtaToGG;
        bufferToObject(&bes3eta,eta);
        recobj->m_evtRecEtaToGGCol->Add(eta);
    }
    recobj->m_evtRecDTagCol->Clear();
    for(int i=0,total=buf->m_evtrecdtagcol_size();i<total;i++){
        const bes3::TEvtRecDTag &bes3dtag=buf->m_evtrecdtagcol(i);
        TEvtRecDTag *dtag=new TEvtRecDTag;
        bufferToObject(&bes3dtag,dtag);
        recobj->m_evtRecDTagCol->Add(dtag);
    }
}
